home *** CD-ROM | disk | FTP | other *** search
- /*
- RGBtoYUV.c - Convert RGB color to YUV color.
-
- MacTech Magazine Programmers' Challenge
- July, 1994
- Written by Robert A. Noll
-
- Copyright (c) 1994 Robert A. Noll
- */
-
- #include "RGBtoYUV.h"
-
- /*
- RGBtoYUV - Calculate the Y, U, and V values for set of
- R, G, and B values input. This function relies
- completely on the data set up by RGBtoYUVInit and
- therefore checks the validity of the data sent. It
- will return with no action if data is not valid.
- */
- void RGBtoYUV(unsigned char *rPtr, /* Red buffer */
- unsigned char *gPtr, /* Green buffer */
- unsigned char *bPtr, /* Blue buffer */
- unsigned char *yPtr, /* Luminance buffer */
- signed char *uPtr, /* U Chrominance buff */
- signed char *vPtr, /* V Chrominance buff */
- unsigned long numPixels,/* Number of Pixels */
- void *privateDataPtr) /* My private data */
- {
- PrivateBlock* pb = (PrivateBlock*)privateDataPtr;
- unsigned long *rp = pb->rp;
- unsigned long *gbp = pb->gbp;
- unsigned char *up = pb->up;
- unsigned char *vp = pb->vp;
- unsigned long tmp;
- union {
- unsigned short i;
- unsigned char c[2];
- } gb;
-
- if (pb->sig != 'RNCS') return;
-
- for (; numPixels>0; --numPixels,++rPtr,++gPtr,++bPtr,
- ++yPtr,++uPtr,++vPtr) {
-
- /* Get index into 'gb' and 'v' tables */
- gb.c[0] = *gPtr;
- gb.c[1] = *bPtr;
-
- /* Calculate Y value from tables */
- tmp = rp[*rPtr] + gbp[gb.i];
- *yPtr = *((unsigned char*)&tmp);
-
- /* Calculate V value from table */
- *vPtr = (*rPtr - vp[gb.i]) >> 1;
-
- /* Get index into 'u' table */
- gb.c[0] = *rPtr;
- gb.c[1] = *gPtr;
-
- #if NegToZero
- /* If for Negs .5 rounds up to 0 then
- * if R == G and R|G > B we calc special.
- */
- if (*rPtr==*gPtr && *gPtr>*bPtr)
- *uPtr = ((*bPtr - *gPtr) + 1) >> 1;
- else
- #endif
- /* Calculate U value from table */
- *uPtr = (*bPtr - up[gb.i]) >> 1;
- }
- }
-